package com.boge.modules.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.boge.common.utils.ShiroUtils;
import com.boge.modules.house.service.TTypeService;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.Query;

import com.boge.modules.house.dao.THouseDao;
import com.boge.modules.house.entity.THouseEntity;
import com.boge.modules.house.service.THouseService;


@Service("tHouseService")
public class THouseServiceImpl extends ServiceImpl<THouseDao, THouseEntity> implements THouseService {

    @Autowired
    private SysUserService userService;

    @Autowired
    private TTypeService typeService;

    private static final String BASE_URL = "http://localhost:8070//base-fast/file/fileController/download?fileName=";

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 管理员可以看所有的数据。房东只能看自己的数据
        SysUserEntity loginUser = ShiroUtils.getUserEntity();

        IPage<THouseEntity> page = this.page(
                new Query<THouseEntity>().getPage(params),
                new LambdaQueryWrapper<THouseEntity>()
                .eq(!"admin".equals(loginUser.getUsername()),THouseEntity::getUserId,loginUser.getUserId())
        );

        List<THouseEntity> records = page.getRecords();
        if(records != null && records.size() > 0){
            for (THouseEntity record : records) {
                Integer userId = record.getUserId();
                SysUserEntity userEntity = userService.getById(userId);
                record.setUserName(userEntity.getNickname());
                record.setTypeName(typeService.getById(record.getTypeId()).getName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    public Map<String,Object> queryPageApp(Map<String, Object> params) {
        // 地区参数
        String districtCode = (String) params.get("districtCode");
        boolean rentFlag = false;
        // 租金价格区间
        String minRent = (String) params.get("minRent");
        String maxRent = (String) params.get("maxRent");
        int minRentInt = 0;
        int maxRentInt = 0;
        if(StringUtils.isNotBlank(minRent) && StringUtils.isNotBlank(maxRent)){
            rentFlag = true;
            maxRentInt = Integer.parseInt(minRent);
            minRentInt = Integer.parseInt(maxRent);
        }
        // 支付方式
        String paymentType = (String) params.get("paymentType");

        // 排序方式 orderType
        String orderBy = (String) params.get("orderBy");
        String orderType = (String) params.get("orderType");

        IPage<THouseEntity> page = this.page(
                new Query<THouseEntity>().getPage(params),
                new LambdaQueryWrapper<THouseEntity>()
                .eq(StringUtils.isNotBlank(districtCode),THouseEntity::getAreaCode,districtCode)
                .between(rentFlag,THouseEntity::getRentPriceListing,minRentInt,maxRentInt)
                .eq(StringUtils.isNotBlank(paymentType),THouseEntity::getPaymentType,paymentType)
                .orderByAsc(THouseEntity::getAreaCode)
        );

        List<THouseEntity> records = page.getRecords();
        // 组装响应的数据
        List<Map<String,Object>> resList = new ArrayList<>();
        if(records != null && records.size() > 0){
            for (THouseEntity record : records) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",record.getId());
                map.put("housePicture",BASE_URL+"/"+record.getImg());
                map.put("houseTitle",record.getName());
                map.put("address",record.getAddress());
                map.put("rentPriceUnit",record.getRentPriceUnit());
                map.put("rentPriceListing",record.getRentPriceListing());
                map.put("rentArea",record.getRentArea());
                // 还需要处理 tag 标签
                String tags = record.getTags();
                List<Map<String,String>> tagsList = new ArrayList<>();
                if(StringUtils.isNotBlank(tags)){
                    if(tags.contains(",")){
                        String[] split = tags.split(",");
                        for (String s : split) {
                            Map<String,String> mapTag = new HashMap<>();
                            mapTag.put("name",s);
                            tagsList.add(mapTag);
                        }
                    }else{
                        Map<String,String> mapTag = new HashMap<>();
                        mapTag.put("name",tags);
                        tagsList.add(mapTag);
                    }
                }
                map.put("tags",tagsList);
                resList.add(map);
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message","success");
        Map<String,Object> mapData = new HashMap<>();
        mapData.put("records",resList);
        mapData.put("total",page.getTotal());
        map.put("data",mapData);
        return map;
    }

}